package com.lishiots.cloud.datacenter.middleware.web;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lishiots.cloud.datacenter.middleware.entity.Document;
import com.lishiots.cloud.datacenter.middleware.entity.Label;
import com.lishiots.cloud.datacenter.middleware.service.DocumentService;
import com.lishiots.cloud.datacenter.middleware.service.LabelService;
import com.lishiots.cloud.datacenter.middleware.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Chaofan at 2018-11-21 16:23
 * email:chaofan2685@qq.com
 **/

@Controller
@RequestMapping("/")
public class JumpController {

    @Autowired
    DocumentService documentService;

    @Autowired
    UserService userService;

    @Autowired
    LabelService labelService;

    @RequestMapping("detail/{docId}")
    public String detail(@PathVariable String docId,Model model){
        if (StrUtil.isEmpty(docId)){
            return "404";
        }
        Document document = documentService.getById(docId);
        if (document == null){
            return "404";
        }
        model.addAttribute("title",document.getTitle());
        model.addAttribute("docId",document.getId());
        model.addAttribute("content",document.getContent());
        model.addAttribute("description",document.getDescription());
        model.addAttribute("gitlaburl",document.getGitlabUrl());
        model.addAttribute("userName",userService.getById(document.getUserId()).getName());
        model.addAttribute("userId",document.getUserId());
        model.addAttribute("contentType",document.getContentType());
        QueryWrapper<Label> wrapper = new QueryWrapper<>();
        wrapper.eq("document_id",document.getId());
        List<String> lableNames = new ArrayList<>();
        labelService.list(wrapper).stream().forEach(label -> lableNames.add(label.getLabelName()));
        model.addAttribute("labels",lableNames);
        return "detail";
    }

    @RequestMapping("update/{docId}")
    public String update(@PathVariable String docId,Model model){
        if (StrUtil.isEmpty(docId)){
            return "404";
        }
        Document document = documentService.getById(docId);
        if (document == null){
            return "404";
        }
        model.addAttribute("title",document.getTitle());
        model.addAttribute("docId",document.getId());
        model.addAttribute("content",document.getContent());
        model.addAttribute("description",document.getDescription());
        model.addAttribute("contentType",document.getContentType());
        model.addAttribute("gitlaburl",document.getGitlabUrl());
        QueryWrapper<Label> wrapper = new QueryWrapper<>();
        wrapper.eq("document_id",document.getId());
        StringBuffer lableNames = new StringBuffer();
        labelService.list(wrapper).stream().forEach(label -> lableNames.append(","+label.getLabelName()));
        model.addAttribute("labels",lableNames.toString().substring(1));
        return "update";
    }

//    @RequestMapping("login/{currentUrl}")
//    public String login(@PathVariable String currentUrl,Model model){
//        model.addAttribute("currentUrl",currentUrl+".html");
//        return "login";
//    }

}
